package xuanzhen.dating.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import xuanzhen.dating.pojo.DatingUser;
import xuanzhen.dating.service.DatingService;
import xuanzhen.dating.service.SeeShareService;
import xuanzhen.wx.pojo.AccessToken;
import xuanzhen.wx.pojo.Jsticket;
import xuanzhen.wx.service.CoreService;
import xuanzhen.wx.util.fina.WxSet;

@Controller
@RequestMapping("/dating")
public class DatingController {
	@Resource
	CoreService coreService;
	@Resource
	DatingService datingService;
	@Resource
	SeeShareService seeService;
	@RequestMapping(value = "/index.html", method = RequestMethod.GET)
	public String index(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String tag =  request.getParameter("spread");
		session.setAttribute("spread", tag);
		
		seeService.addweShare(tag);
			String spread =  request.getParameter("spread");
			model.addAttribute("spread", spread);
		return "dating/dating2";
	}

	@RequestMapping(value = "/re.html", method = RequestMethod.GET)
	public void toRight(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String tag =  request.getParameter("spread");
		session.setAttribute("spread", tag);
		
		seeService.addweShare(tag);
		/*if(tag == "wechat" | tag == "wechatshare"){*/
			// 当前网页url
			String url = "";
			/*url = request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort() + request.getServletPath();*/
			if (request.getQueryString() != null) {
				url += "?" + request.getQueryString();
			} 
			response.sendRedirect(coreService.getUser(url));
			/*return null;*/
		/*}else{
			return "dating/dating2";
		}*/
	}
	/**
	 * 获取授权
	 */
	@RequestMapping(value = "/registered.html", method = RequestMethod.GET)
	public String getRight(Model model, HttpSession session, HttpServletRequest request) throws Exception {
		String tag =  request.getParameter("spread");
		session.setAttribute("spread", tag);
		System.out.println(tag);
		// 获取授权后code
		/*String code = request.getParameter("code");
		session.setAttribute("code", code);
		String state = request.getParameter("state");*/
		// 获得js票
				AccessToken tokenCe = coreService.getAccessTokenC();
				Jsticket ticket = coreService.getJsticket(tokenCe);
				session.setAttribute("ticket", ticket);
				System.out.println(ticket);
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
				String nowTime = dateFormat.format(now);
				// 获取时间戳
				long timestamp = System.currentTimeMillis() / 1000;
				session.setAttribute("timestamp", timestamp);
				// 生成随机字符串
				String noncestr = RandomStringUtils.randomAlphanumeric(16);
				session.setAttribute("noncestr", noncestr);
				
				// appid
				session.setAttribute("appid", WxSet.APPID);
				// 当前网页url
				String url = "";
				url = request.getScheme() + "://" + request.getServerName() + request.getRequestURI();
				if (request.getQueryString() != null) {
					url += "?" + request.getQueryString();
				} 		
				System.out.println(url);
					/*	"http://run.xuanzhen-tech.com/wx_project/user/getRight.html?code=" + code + "&state=STATE";*/
				// 签名
				String signature = coreService.getSignature(url, timestamp + "", noncestr, ticket.getTicket());
				session.setAttribute("signature", signature);
		//判定11号
				if(datingService.countRecordsBySignTime("11.11")>=400){
					model.addAttribute("nohave11", "no");
				}

		return "dating/dating2";
	}
	@RequestMapping(value = "/signIn.html", method = RequestMethod.POST)
	public Object add(Model model, HttpSession session,DatingUser datingUser, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String gend = request.getParameter("gender");
		String mem = request.getParameter("member");
		datingUser.setGender(gend);
		datingUser.setMember(mem);
		System.out.println(datingUser.getGender());
		datingService.addDatingUser(datingUser);
		return null;
	}
	@RequestMapping(value = "/wechatShare", method = RequestMethod.POST)
	public void countshare(Model model, HttpSession session,DatingUser datingUser, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		datingService.addweShare(datingUser);
		/*datingService.addshareCount(s);*/

	}
}
